package jjoms.aop.common;

import javax.annotation.Resource;

import jjoms.aop.model.GetRankService;
import jjoms.aop.model.RankVO;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AopCheck {
	 @Resource(name = "getRankServiceImpl")
     private GetRankService getRankService;
	 private int no;
	 private String keyword;
	 @AfterReturning("execution(public * jjoms.controller.ItemController.showItemContent(..))")
	 public void before(JoinPoint jointPoint){
			Object param[]=jointPoint.getArgs();// 메서드 인자값 - 매개변수
			no = Integer.parseInt(param[0].toString());
	 }
	
	@After("execution(public * jjoms.controller.ItemController.fileDownload(..))")
	public void after(JoinPoint jointPoint ){
		String keyword = getRankService.getTitle(no);
		System.out.println("**************************************************");
		System.out.println("getRank keyword: " + no + "key = " + keyword);
		System.out.println("**************************************************");
		
		//랭크 등록
		getRankService.setRank(keyword, "item");
	}
	
	/**
	 * jjoms.model.info.InfoServiceImpl.findByKeyword(..)) 해당 메서드 실행시 Exception 을 발생시켜 aop 적용
	 * @param joinPoint
	 * @param ex
	 */
	@AfterThrowing(pointcut = "execution(public * jjoms.model.info.InfoServiceImpl.findByKeyword(..))", throwing = "ex")
	public void hitKeyword(JoinPoint joinPoint, Throwable ex){
		String keyword = ex.getMessage();
		
		String targetClass = joinPoint.getTarget().getClass().getName();
		String methodName = joinPoint.getSignature().getName();
		Object param[]=joinPoint.getArgs();// 메서드 인자값 - 매개변수
		keyword = param[0].toString();
		
		
		System.out.println("**************************************************");
		System.out.println("대상 클래스  : " + targetClass);
		System.out.println("대상 메서드 : " + methodName);
		System.out.println("findByKeyword 에 입력된 keyword : " + keyword);
		System.out.println("findByKeyword 에서 반환된 ex : " + ex);
		System.out.println("findByKeyword 에서 반환된 keyword : " + keyword); // super 에 입력된 String 값
		System.out.println("**************************************************");

		getRankService.setRank(keyword, "info");
		System.out.println("info~update성공");
	}
	@AfterThrowing(pointcut = "execution(public * jjoms.model.main.*ServiceImpl.*(..))", throwing = "ex")
	public void hitKeywordToAllSearch(JoinPoint joinPoint, Throwable ex){
		String keyword = ex.getMessage();
		
		String targetClass = joinPoint.getTarget().getClass().getName();
		String methodName = joinPoint.getSignature().getName();
		Object param[]=joinPoint.getArgs();// 메서드 인자값 - 매개변수
		keyword = param[0].toString();
		
		
		System.out.println("**************************************************");
		System.out.println("대상 클래스  : " + targetClass);
		System.out.println("대상 메서드 : " + methodName);
		System.out.println("findByKeyword 에 입력된 keyword : " + keyword);
		System.out.println("findByKeyword 에서 반환된 ex : " + ex);
		System.out.println("findByKeyword 에서 반환된 keyword : " + keyword); // super 에 입력된 String 값
		System.out.println("**************************************************");

		getRankService.setRank(keyword, "info");
		System.out.println("info~update성공");
	}
	
}
